/**
* @Title: integrityMedicalDialog_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年11月3日 下午9:21:08
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('IntegrityMedicalDialogCtrl', ['$scope', '$http','BaseURL','CommunityService', 'Modal','$modalInstance','IntegrityMedicalService', function($scope, $http, baseUrl,CommunityService, Modal,$modalInstance,IntegrityMedicalService){
		$scope.integrityMedicalService = IntegrityMedicalService;
		IntegrityMedicalService.initMedicalOrgInfoList();
		$scope.formDialog = {};
		//初始化搜索框值
		$scope.formDialog.name="";
		if($scope.form.doctorName){
			var arrs = add(IntegrityMedicalService.ownerIntegritys, IntegrityMedicalService.systemIntegritys);
        	IntegrityMedicalService.ownerIntegritys = arrs[0];
        	IntegrityMedicalService.systemIntegritys = arrs[1];
		}
		var params = function (){
			var doctorName=null;
			if($scope.formDialog.name&&!angular.isUndefined($scope.formDialog.name)){
				doctorName = $scope.formDialog.name;
			}
			var practiceLicense=null;
			if($scope.formDialog.practiceLicense&&!angular.isUndefined($scope.formDialog.practiceLicense)){
				practiceLicense = $scope.formDialog.practiceLicense;
			}
			var orgCodeDialog=null;
			if($scope.formDialog.orgCodeDialog&&!angular.isUndefined($scope.formDialog.orgCodeDialog)){
				orgCodeDialog = $scope.formDialog.orgCodeDialog;
			}
			var certificateNo=null;
			if($scope.formDialog.certificateNo&&!angular.isUndefined($scope.formDialog.certificateNo)){
				certificateNo = $scope.formDialog.certificateNo;
			}
			var params = {
					doctorName:doctorName,
					practiceLicense:practiceLicense,
					orgCode:orgCodeDialog,
					certificateNo:certificateNo
			}
			return params;
		}
		$scope.queryMedical = function (){
			var isNameEmpty=!$scope.formDialog.name;
			var isPracticeLicense=!$scope.formDialog.practiceLicense;
			var isOrgCodeDialog=!$scope.formDialog.orgCodeDialog;
			var isCertificateNo=!$scope.formDialog.certificateNo;
			if(isNameEmpty&&isPracticeLicense&&isOrgCodeDialog&&isCertificateNo){
				Modal.alert("必须输入一个查询条件！");
				return null;
			}
			IntegrityMedicalService.getIntegrityDoctor(params()).
			success(function(res){
				IntegrityMedicalService.systemIntegritys = res;
				if(!res){
					IntegrityMedicalService.ownerIntegritys = [];
				}
			});
		}
		$scope.$watch('formDialog.name', function(e,v){
			if($scope.formDialog.name){
				$scope.queryMedical();
			}
		});
		function init(src, tar) {
			var selected = [], unselected = [];
			$.each(src, function(){
				if(this.selected) {
					selected.push(this);
					this.selected = false;
				} else {
					unselected.push(this);
				}
			})
			return [unselected, tar.concat(selected)];
		}
		function add(src, tar) {
			var selected = [], unselected = [];
			$.each(src, function(){
				if(this.selected) {
					selected.push(this);
					this.selected = false;
				} else {
					unselected.push(this);
				}
			})
			return [unselected, tar.concat(selected)];
		}
		function addAll(src, tar) {
			return $.grep(src, function(entry) {
				if ($.inArray(entry, tar) != -1) {
					return true;
				}
				entry.selected = false;
				tar.push(entry);
				return false;
			});
		}
		
		function delAll(src, tar) {
			return $.grep(src, function(entry) {
				if ($.inArray(entry, tar) != -1) {
					return true;
				}
				entry.selected = false;
				tar.push(entry);
				return false;
			});
		}
		$scope.selectUserInfoList= [];
		//关闭model
		$scope.onClose = function() {
			$modalInstance.close();
		}
		//添加所有
		$scope.addAll=function(){
			IntegrityMedicalService.systemIntegritys = addAll(IntegrityMedicalService.systemIntegritys, IntegrityMedicalService.ownerIntegritys);
		} 
        $scope.add=function(){
        	var arrs = add(IntegrityMedicalService.systemIntegritys, IntegrityMedicalService.ownerIntegritys);
        	IntegrityMedicalService.systemIntegritys = arrs[0];
        	IntegrityMedicalService.ownerIntegritys = arrs[1];
		}
        $scope.del=function(){
        	var arrs = add(IntegrityMedicalService.ownerIntegritys, IntegrityMedicalService.systemIntegritys);
        	IntegrityMedicalService.ownerIntegritys = arrs[0];
        	IntegrityMedicalService.systemIntegritys = arrs[1];
		}
        $scope.delAll=function(){
        	IntegrityMedicalService.ownerIntegritys = delAll(IntegrityMedicalService.ownerIntegritys, IntegrityMedicalService.systemIntegritys);
		} 
	      //确定按钮事件
			$scope.selectMedicalOrg = function() {
				$modalInstance.close(IntegrityMedicalService.ownerIntegritys);
			 }
			
		$modalInstance.result.then(function (selectedItem) {
		    	//确定
		    	if(selectedItem){
		    	 var nameArrs=new Array();
		    	 var codeArrs=new Array();
		    	 $.each(selectedItem,function(index,item){
		    		 nameArrs.push(item.select);
		    		 codeArrs.push(item.code);
		    	 });
		    	 $scope.form.doctorCode = "";
		    	 $scope.form.doctorName = "";
		    	 $scope.form.doctorCode=codeArrs.toString();
		    	 $scope.form.doctorName=nameArrs.toString();
		    	}
		    });
		
		$scope.onIntegrityItemClick = function(integrity) {
			integrity.selected = integrity.selected == true?false:true;
		}
		//重置
		$scope.onReset = function (){
			$scope.formDialog = {};
		}
	}]);
})();